Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose a pointer of EventLoopProxy to process custom messages #674

Merged
merged 1 commit into from
Oct 15, 2020

Conversation

J-F-Liu
Copy link
Contributor

@J-F-Liu J-F-Liu commented Oct 13, 2020

An exmaple usage is:

fn main() {
    App::build()
        ................
        .add_system(process_messages.thread_local_system())
        ................
        .run();
}

fn process_messages(_world: &mut World, resources: &mut Resources) {
    use bevy::window::WindowCreated;
    use bevy::winit::{EventLoopProxyPtr, WinitWindows};
    use winit::platform::windows::WindowExtWindows;

    let mut window_created_event_reader = EventReader::<WindowCreated>::default();
    let window_created_events = resources.get::<Events<WindowCreated>>().unwrap();
    if let Some(window_created) = window_created_event_reader.latest(&window_created_events) {
        if window_created.id.is_primary() {
            let winit_windows = resources.get::<WinitWindows>().unwrap();
            let event_proxy_ptr = resources.get_thread_local::<EventLoopProxyPtr>().unwrap();
            let main_window = winit_windows.get_window(window_created.id).unwrap();
            dbg!(main_window.hwnd());
            unsafe {
                let res = commctrl::SetWindowSubclass(
                    main_window.hwnd() as HWND,
                    Some(subclass_proc),
                    0x10000,
                    event_proxy_ptr.0 as DWORD_PTR,
                );
                dbg!(res);
            }
        }
    }
}

use winapi::{
    shared::{
        basetsd::{DWORD_PTR, UINT_PTR},
        minwindef::{LPARAM, LRESULT, UINT, WPARAM},
        windef::HWND,
    },
    um::commctrl,
};

unsafe extern "system" fn subclass_proc(
    hwnd: HWND,
    msg: UINT,
    wparam: WPARAM,
    lparam: LPARAM,
    _id: UINT_PTR,
    _data: DWORD_PTR,
) -> LRESULT {
    match msg {
        6012 => {
            println!("custom message received");
            0
        }
        _ => commctrl::DefSubclassProc(hwnd, msg, wparam, lparam),
    }
}

For more info, see rust-windowing/winit#1052

@karroffel karroffel added A-ECS Entities, components, systems, and events C-Enhancement A new feature A-Windowing Platform-agnostic interface layer to run your app in labels Oct 14, 2020
@cart
Copy link
Member

cart commented Oct 15, 2020

Seems reasonable to me. I didn't know this was a thing :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events A-Windowing Platform-agnostic interface layer to run your app in C-Enhancement A new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants